/**
 * Copyright (c) 2019-2029, wwww.kiwipeach.cn (liuburu@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cn.kiwipeach.blog;

import cn.kiwipeach.blog.utils.SpringUtil;
import cn.kiwipeach.blog.utils.SystemRuntimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.servlet.Cookie;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.text.MessageFormat;


/**
 * 持久层启动类入口
 *
 * @author kiwipeach
 * @since 1.0
 */
@EnableTransactionManagement
@SpringBootApplication(scanBasePackages = {"cn.kiwipeach.blog"})
@EnableCaching
@MapperScan(value = {"cn.kiwipeach.blog.database.generator.mapper", "cn.kiwipeach.blog.database.mapper"})
@Slf4j
public class BlogWebApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BlogWebApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(BlogWebApplication.class);
        app.setBannerMode(Banner.Mode.CONSOLE);
        app.run(args);
        Cookie cookie = SpringUtil.getBean("shiroCookie", Cookie.class);
        ServerProperties serverProperties = SpringUtil.getBean(ServerProperties.class);
        afterLauchAction(cookie.getDomain(), serverProperties.getPort(), serverProperties.getServlet().getContextPath());
    }

    private static void afterLauchAction(String domain, Integer port, String contextPath) {
        StringBuffer hostBuffer = new StringBuffer("http://").append(domain).append(":").append(port);
        StringBuffer swaggerUrl = new StringBuffer(String.valueOf(hostBuffer)).append(contextPath).append("/swagger-ui.html");
        StringBuffer druidUrl = new StringBuffer(String.valueOf(hostBuffer)).append(contextPath).append("/druid");
        log.warn("系统首页:{}", swaggerUrl.toString());
        log.warn("Druid首页:{}", druidUrl.toString());
        if (isWindowsPlatform()) {
            SystemRuntimeUtil.browerOpen(String.valueOf(swaggerUrl));
        }
    }

    /**
     * 是否为window平台
     *
     * @return 返回是否windows或者linux等
     */
    private static boolean isWindowsPlatform() {
        return System.getProperty("os.name").contains("Windows");
    }

}
